package com.ruoyi.develop.lvRukumingxi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.lvRuku.domain.LvRuku;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.ILvRukumingxiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 出入库记录Controller
 * @author qh
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/lvRukumingxi/lvRukumingxi")
public class LvRukumingxiController extends BaseController {
    @Autowired
    private ILvRukumingxiService lvRukumingxiService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;


    /**
     * 查询出入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvRukumingxi entity) {
        startPage();
        List<LvRukumingxi> list = lvRukumingxiService.selectList(entity);
        return getDataTable(list);
    }



    /**
     * 查询出采购明细 可用数量大于0的
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:query')")
    @GetMapping("/listRemainNotZero")
    public TableDataInfo listRemainNotZero(LvRukumingxi entity) {
        startPage();
        List<LvRukumingxi> list = lvRukumingxiService.selectListRemainNotZero(entity);
        return getDataTable(list);
    }


    /**
     * 导出出入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:export')")
    @Log(title = "出入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvRukumingxi entity) {
        List<LvRukumingxi> list = lvRukumingxiService.selectList(entity);
        ExcelUtil<LvRukumingxi> util = new ExcelUtil<LvRukumingxi>(LvRukumingxi. class);
        util.exportExcel(response, list, "出入库记录数据");
    }

    /**
     * 获取出入库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvRukumingxiService.selectById(id));
    }

    /**
     * 新增出入库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:add')")
    @Log(title = "出入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.insert(entity));
    }

    /**
     * 新增入库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:add')")
    @Log(title = "新增入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/addIn")
    public AjaxResult addIn(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.insertIn(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:add')")
    @Log(title = "自制产品新增入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/addInZizhi")
    public AjaxResult addInZizhi(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.addInZizhi(entity));
    }

    /**
     * 新增出库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:add')")
    @Log(title = "新增入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/addOut")
    public AjaxResult addOut(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.insertOut(entity));
    }

    /**
     * 修改出入库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:edit')")
    @Log(title = "出入库记录", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:edit')")
    @Log(title = "自制产品入库驳回", businessType = BusinessType.UPDATE)
    @PutMapping("/editBohui")
    public AjaxResult editBohui(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.editBohui(entity));
    }
    /**
     * 删除出入库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:remove')")
    @Log(title = "出入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvRukumingxiService.deleteByIds(ids));
    }

    /**
     * 删除出入库记录
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:remove')")
    @Log(title = "出入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeDetail/{ids}")
    public AjaxResult removeDetail(@PathVariable String[] ids) {
        for(String id:ids){
            LvRukumingxi detail = lvRukumingxiService.selectById(id);
            GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(detail.getGoodsId());
            if(detail.getType().equals(BusinessConstants.TYPR_CHUKU)){
                goodsOrderIn.setCount(goodsOrderIn.getCount().add(detail.getDaohuoCount()));
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(detail.getDaohuoCount()));
            }
            if(detail.getType().equals(BusinessConstants.TYPE_RUKU)){
                goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(detail.getDaohuoCount()));
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(detail.getDaohuoCount()));
            }
            goodsOrderInService.update(goodsOrderIn);
        }
        return toAjax(lvRukumingxiService.deleteByIds(ids));
    }

     @Log(title = "自制入库", businessType = BusinessType.DELETE)
    @GetMapping("/ruku/{ids}")
    public AjaxResult ruku(@PathVariable String[] ids) {
        return AjaxResult.success(lvRukumingxiService.ruku(ids));
    }

    @Log(title = "自制质检合格", businessType = BusinessType.DELETE)
    @GetMapping("/zhijianSuccess/{ids}")
    public AjaxResult zhijianSuccess(@PathVariable String[] ids) {
        return AjaxResult.success(lvRukumingxiService.zhijianSuccess(ids));
    }
    @Log(title = "自制驳回提交到质检", businessType = BusinessType.DELETE)
    @GetMapping("/zizhiToZhijian/{ids}")
    public AjaxResult zizhiToZhijian(@PathVariable String[] ids) {
        return AjaxResult.success(lvRukumingxiService.zizhiToZhijian(ids));
    }

    @Log(title = "产成品入库导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadChanchengpin")
    public AjaxResult uploadChanchengpin(LvRukumingxi entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = lvRukumingxiService.uploadChanchengpin(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }
    @Log(title = "采购入库导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadCaigou")
    public AjaxResult uploadCaigou(LvRukumingxi entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = lvRukumingxiService.uploadCaigou(entity, file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }
    @Log(title = "其他出库导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadOther")
    public AjaxResult uploadOther(LvRukumingxi entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = lvRukumingxiService.uploadOther(entity, file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }
    @Log(title = "发货单出库导入", businessType = BusinessType.INSERT)
    @PostMapping("/uploadOut")
    public AjaxResult uploadOut(LvRukumingxi entity, MultipartFile file) {
        Map<String, Object> error = new HashMap<>();
        error = lvRukumingxiService.uploadOut(entity,file);
        error.put("code", 200);
        return AjaxResult.success(error);
    }



    /**
     * 查询 采购的 未开票列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvRukumingxi:query')")
    @GetMapping("/buyNotInvoiceList")
    public TableDataInfo buyNotInvoiceList(LvRukumingxi entity) {
        startPage();
        List<LvRukumingxi> list = lvRukumingxiService.buyNotInvoiceList(entity);
        return getDataTable(list);
    }

    @PostMapping("/addRukumingxiList")
    public AjaxResult addRukumingxiList(@RequestBody LvRukumingxi entity) {
        return toAjax(lvRukumingxiService.addRukumingxiList(entity));
    }

    /**
     * 导出入库明细
     */
    @Log(title = "导出入库明细", businessType = BusinessType.EXPORT)
    @GetMapping("/exportIn")
    public void exportIn(HttpServletRequest request, HttpServletResponse response, LvRukumingxi entity) {
        lvRukumingxiService.exportDataIn(request,response,entity);
    }

    /**
     * 导出出库明细
     */
    @Log(title = "导出出库明细", businessType = BusinessType.EXPORT)
    @GetMapping("/exportOut")
    public void exportOut(HttpServletRequest request, HttpServletResponse response, LvRukumingxi entity) {
        lvRukumingxiService.exportDataOut(request,response,entity);
    }

    // 删除物料单产品
    @DeleteMapping("/deleteRukumingxi/{ids}")
    public AjaxResult deleteRukumingxi(@PathVariable String[] ids) {
        return toAjax(lvRukumingxiService.deleteRukumingxi(ids));
    }
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaleMaterialMain:add')")
    @Log(title = "创建出库单", businessType = BusinessType.INSERT)
    @PostMapping("/addMainOtherChuku")
    public AjaxResult addMainOtherChuku(@RequestBody LvRuku entity) {
        return toAjax(lvRukumingxiService.addMainOtherChuku(entity));
    }


}
